package io.bloom.webspider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public interface Processor
{
    void execute(SpiderResponse response, Map<String, Object> context);

    int index();

    Processor console = new Processor()
    {
        private Logger log = LoggerFactory.getLogger("console");

        public void execute(SpiderResponse response, Map<String, Object> context)
        {
            String rawText = response.getRawText();
            if (null != rawText)
            {
                if (rawText.length() > 300)
                {
                    String text = rawText.replaceAll("\\s+", "");
                    rawText = text.substring(0, text.length() > 200 ? 200 : text.length());
                }
                log.info("response text:{}", rawText);
            }

            if (null != response.getRawBinary())
            {
                log.info("response binary size: {}", response.getRawBinary().length);
            }
        }

        public int index()
        {
            return 0;
        }
    };
}
